// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class FluxayShader extends cc.Component {

    sp: cc.Sprite = null;   //当前节点的图片
    timer = 0;       //当前循环的时间
    onLoad() {
        this.sp = this.node.getComponent(cc.Sprite);
    }
    // LIFE-CYCLE CALLBACKS:

    update(dt) {
        this.timer += dt;
        this.setTimer();
    }

    setTimer() {
        // 从精灵中clone出精灵的材质对象;
        // (注意是clone, 另外一个材质对象)
        let material = this.sp._materials[0];
        // 设置材质对象的全局属性变量;
        material.setProperty('time', this.timer);
        // 将材质对象设置为Sprite的材质对象;
        this.sp.setMaterial(0, material);

    }
}
